//
//  MSStageViewController.m
//  SocietySale
//
//  Created by Mike on 16/11/4.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSStageViewController.h"

@interface MSStageViewController ()
@property (nonatomic, copy) NSString        *stageFile;
@property (nonatomic, copy) NSString        *codeFile;
@property (nonatomic, strong) NSMutableArray*titleArray;    //标题
@property (nonatomic, strong) NSMutableArray*stateArray;     //数据
@property (nonatomic, strong) NSMutableArray* codeArray;     //数据
@end

@implementation MSStageViewController


- (void)initNavigationBar{
    [super initNavigationBar];
    leftBarBtn.hidden = NO;
    rightBarBtn.hidden = NO;
    [rightBarBtn setTitle:@"确定" forState:UIControlStateNormal];
    [rightBarBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightBarBtn.titleLabel.font = HeitiFont(14);
    titleLable.text = @"项目阶段";
}

- (void)initUI{
    self.contentView.scrollEnabled = YES;
    NSString*documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];

//    _titleArray = [[NSMutableArray alloc] initWithObjects:@"工程筹备", @"勘察设计", @"主体施工", @"设备安装", @"装饰装修", @"园林绿化", @"竣工测试", nil];
    _titleArray = [[NSMutableArray alloc] initWithCapacity:0];
    _codeArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (NSDictionary *dict in netEngine.projectStateArray) {
        if ([dict isKindOfClass:[NSDictionary class]]) {
            [_titleArray addObject:[HDUtil check:dict[@"name"]]];
            [_codeArray addObject:[HDUtil check:dict[@"code"]]];
        }
    }
//    _codeArray = _titleArray;
    self.stageFile = [documentsPath stringByAppendingPathComponent:@"preferenceStageFile.plist"];
    self.codeFile = [documentsPath stringByAppendingPathComponent:@"stageCodeFile.plist"];
    NSFileManager* fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:self.stageFile]) {
        self.stateArray = [[NSMutableArray alloc] initWithContentsOfFile:self.stageFile];
    }
    else{
        self.stateArray =  [[NSMutableArray alloc] initWithCapacity:0];
        for (int i=0; i<_titleArray.count; i++) {
            [self.stateArray addObject:@"NO"];
        }
    }
    for (int i=0; i<_titleArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(85, 25+i*(44+24), SCREEN_WIDTH-170, 44);
        btn.tag = 100+i;
        btn.titleLabel.font = HeitiFont(14);
        [btn setTitle:_titleArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.layer.cornerRadius = 2;
        btn.layer.borderWidth = 1;
        btn.layer.borderColor = kNormalGray.CGColor;
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:btn];
        
        UIImageView *arrow = [[UIImageView alloc] init];
        arrow.frame = CGRectMake((SCREEN_WIDTH-15)/2, btn.bottom+7, 15, 10);
        arrow.image = [UIImage imageNamed:@"arrow1"];
        [self.contentView addSubview:arrow];
        
        if (i == _titleArray.count-1) {
            arrow.hidden = YES;
        }
        
        NSString *stateStr = _stateArray[i];
        if ([stateStr isEqualToString:@"YES"]) {
            btn.selected = NO;
        }
        else{
            btn.selected = YES;
        }
        [self click:btn];
    }
    self.contentView.contentSize = CGSizeMake(SCREEN_WIDTH, _titleArray.count*68+24);
}

- (void)testDataFunc{
    
}

- (void)rightBarBtnAction{
    NSMutableArray *relutArray = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *tempCodeArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i=0; i<_stateArray.count; i++) {
        NSString *stateStr = _stateArray[i];
        if ([stateStr isEqualToString:@"YES"]) {
            [relutArray addObject:_titleArray[i]];
            [tempCodeArray addObject:_codeArray[i]];
        }
    }
    [_stateArray writeToFile:self.stageFile atomically:YES];
    [tempCodeArray writeToFile:self.codeFile atomically:YES];
    
    if (self.ChoicBlock) {
        self.ChoicBlock(relutArray);
    }
    [rootNavigation popViewControllerAnimated:YES];
    [[NSNotificationCenter defaultCenter] postNotificationName:KPreferenceStageChange object:self];
}

- (void)click:(UIButton*)btn{
    btn.selected = !btn.selected;
    NSInteger btnIndex = btn.tag - 100;
    if (btn.selected) {
        [btn setTitleColor:kBlueColor forState:UIControlStateNormal];
        btn.layer.borderColor = kBlueColor.CGColor;
        [self.stateArray replaceObjectAtIndex:btnIndex withObject:@"YES"];
    }
    else{
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.layer.borderColor = kNormalGray.CGColor;
        [self.stateArray replaceObjectAtIndex:btnIndex withObject:@"NO"];
    }
}

@end
